/* 
  Copyright (c) SilverLining 2015
  Date:    2016-1-7
  File:    weak.cpp  
  Description: 

*/

#include "stdafx.h"

using namespace m::alloc;

namespace m
{
	object* weak_ref::lock() volatile
	{
		if (_Value)
		{
			_Value->reference();
			// We referenced it atomically, so it's safe to ignore potential side effects of reordering instructions.
			return const_cast<object*>(_Value);
		}
		return nullptr;
	}
	
	void * weak_ref::operator new(size_t sz)
	{
		return ::operator new(sz, pool);
	}
		
	void weak_ref::operator delete(void * ptr)
	{
		::operator delete(ptr, pool);
	}

	weak_ref::weak_ref(volatile object * obj)
		: _Value(obj)
	{
	}
} // m